package com.example.lenovo.downloaddemo.activity;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Request;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.lenovo.downloaddemo.R;
import com.example.lenovo.downloaddemo.adapter.MoreAdapter;
import com.example.lenovo.downloaddemo.bean.Customer;
import com.example.lenovo.downloaddemo.bean.ProductBean;
import com.example.lenovo.downloaddemo.http.datasource.MDataSource;
import com.example.lenovo.downloaddemo.http.okhttp.utils.AppContext;
import com.example.lenovo.downloaddemo.http.okhttp.utils.Collintion;
import com.example.lenovo.downloaddemo.http.okhttp.utils.CommonUtils;
import com.example.lenovo.downloaddemo.http.okhttp.utils.MD5;
import com.example.lenovo.downloaddemo.http.okhttp.utils.ToastUtil;
import com.example.lenovo.downloaddemo.sqlite.LoadListCallBack;
import com.example.lenovo.downloaddemo.util.StatusBarUtil;
import com.example.lenovo.downloaddemo.util.TextStyleLanguage;
import com.example.lenovo.downloaddemo.util.TimeUtil;
import com.example.lenovo.downloaddemo.view.TitleBarView;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

public class MoreActivity extends BaseActivity {

    @Bind(R.id.more_ListView)
    ListView listView;
    @Bind(R.id.more_titbar)
    TitleBarView titleBarView;

    private List<ProductBean.DataBean.GoodsListBean> data;
    private MoreAdapter adapter;
    private String allString;

    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                String returnMessage= (String) msg.obj;
                Gson gson=new Gson();
                try {
                    ProductBean bean=gson.fromJson(returnMessage,ProductBean.class);

                        titleBarView.setTitle(bean.getData().getName());
                    if(bean.getCode()==200){
                        data=bean.getData().getGoodsList();
                        adapter=new MoreAdapter(AppContext.getInstance(),data);
                        listView.setAdapter(adapter);



                    }else{
                        ToastUtil.makeText(AppContext.getInstance(),"Fail!");
                    }
                }catch (Exception e){
                    ToastUtil.makeText(AppContext.getInstance(),e.getMessage());
                }



            }





        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);
        ButterKnife.bind(this);

        initView();
    }

    @Override
    protected void initView() {
        StatusBarUtil.setMargin(AppContext.getInstance(),titleBarView);
        titleBarView.showBackButton(true);
        if(CommonUtils.isNetOk(AppContext.getInstance())){

             initData();
        }else{
            ToastUtil.makeText(AppContext.getInstance(),"Fail!");
        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               Log.e("GOODS_ID", data.get(i).getGood_id()+"");
               Intent intent=new Intent(MoreActivity.this,ProductInfoActivity.class);
               intent.putExtra("goodsid",data.get(i).getGood_id());
               startActivity(intent);
            }
        });


    }

    private void initData() {
        Intent intent=getIntent();
        int c_id=intent.getIntExtra("c_id",0);
        Log.e("c_id===",c_id+"");

        /**
         * 建立请求表单，添加上传服务器的参数
         */
        HashMap<String, String> map = new HashMap<>();
        map.put("model", "index");
        map.put("method", "categoryByCidList");
        map.put("c_id",c_id+"");

        map.put("time",new TimeUtil().getMoment());
        Collintion collintion=new Collintion();

        allString=collintion.Sort(map)+"ad1af567dc7bfedd5e760fc1f48a59ae";
        String sign= MD5.GetMD5Code(MD5.GetMD5Code(allString));
        Log.e("拼接字符串",allString);
        Log.e("签名===",sign);
        map.put("sign",sign);

        MDataSource<Customer> dataSource = new MDataSource<>(AppContext.getInstance());
        dataSource.postData(map, new LoadListCallBack() {
            @Override
            public void loadList(List list) {

            }

            @Override
            public void systemError(Request request, String errorInfo, Exception e) {
                Log.e("error", "systemError: " + errorInfo);
                e.printStackTrace();
            }

            @Override
            public void retLoad(String code) {

            }

            @Override
            public void loadString(String string) {
                Log.e("产品列表", "loadString 获取结果 : " + string);

                Message message=Message.obtain();
                message.what=1;
                message.obj=string;
                mHandler.sendMessage(message);





            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        data.clear();
        finish();
    }
}
